package com.hjc.oj.model.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.hjc.oj.model.dto.question.JudgeConfig;
import com.hjc.oj.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Data
public class QuestionVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;
    /**
     * 用户解答的模板
     */
    private String codeTemplate;

    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptedNum;

    /**
     * 判题配置（json 对象）
     */
    private JudgeConfig judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户
     */
    private UserVO userVO;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 包装类转换为对象
     * @param questionVO
     * @return Question
     */
    public static Question voToObj(QuestionVO questionVO){
        if(questionVO == null){
            return  null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO ,question);
        List<String> tagList = questionVO.getTags();
        if(tagList != null){
            question.setTags(JSONUtil.toJsonStr(tagList));
        }
        JudgeConfig voJudgeConfig = questionVO.getJudgeConfig();
        if(voJudgeConfig != null){
            question.setJudgeConfig(JSONUtil.toJsonStr(voJudgeConfig));
        }
        return question;
    }

    /**
     * 对象转包装类
     * @param question
     * @returnQ  QuestionVO
     */
    public static QuestionVO objToVo(Question question){
        if(question == null){
            return null;
        }
        QuestionVO  questionVO = new QuestionVO();
        BeanUtils.copyProperties(question,questionVO);
        questionVO.setTags(JSONUtil.toList(question.getTags(), String.class));
        String judgeConfigList = question.getJudgeConfig();
        questionVO.setJudgeConfig(JSONUtil.toBean(judgeConfigList,JudgeConfig.class));
        return questionVO;
    }

    private static final Long serialVersionUID = 1L;

}
